package com.senlingspace.system.service.impl;

import com.senlingspace.common.utils.NumberGenerator;
import com.senlingspace.system.domain.SlkjCustomer;
import com.senlingspace.system.mapper.SlkjCustomerMapper;
import com.senlingspace.system.service.ISlkjCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 会员信息表 业务层实现
 *
 * @author lebronjames
 * @date 2024/09/26
 */
@Slf4j
@Service
public class SlkjCustomerServiceImpl implements ISlkjCustomerService {

    @Autowired
    private SlkjCustomerMapper slkjCustomerMapper;


    @Override
    public SlkjCustomer getCustomerByPhoneExt(String phoneNumber, String openId) {
        log.info("获取 {} 会员信息", phoneNumber);
        SlkjCustomer customer = slkjCustomerMapper.getCustomerByPhone(phoneNumber);
        if (customer != null) {
            customer.setOpenId(openId);
            slkjCustomerMapper.updateCustomerByCustomerId(customer);
            return customer;
        }

        log.info("未找到 {} 会员信息，执行注册逻辑", phoneNumber);
        customer = new SlkjCustomer();
        customer.setPhone(phoneNumber);
        customer.setOpenId(openId);
        customer.setNickname("森灵" + NumberGenerator.getCharacters(5));
        slkjCustomerMapper.insertCustomer(customer);
        return customer;
    }

}
